package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.CommonUtil;
import io.github.talelin.latticy.common.LocalUser;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.dto.CreateCommonJobDTO;
import io.github.talelin.latticy.dto.UpdateCommonJobDTO;
import io.github.talelin.latticy.entity.CommonJob;
import io.github.talelin.latticy.mapper.CommonJobMapper;
import io.github.talelin.latticy.model.CommonJobDO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.repository.CommonJobRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CommonJobService extends ServiceImpl<CommonJobMapper, CommonJobDO> {

    @Autowired
    private CommonJobRepository commonJobRepository;

    @Autowired
    private JobService jobService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private SettingService settingService;

    public void edit(Long id, CreateCommonJobDTO dto) {
        CommonJob commonJob = this.get(id);
        if (commonJob.getIsGetting()) {
            throw new ForbiddenException(20005);
        }
        userUtil.userAuthority();
        commonJob.setEndTime(CommonUtil.stringToDate2(dto.getEndTime()));
        BeanUtils.copyProperties(dto, commonJob);
        commonJobRepository.save(commonJob);
    }

    public void create(CreateCommonJobDTO dto) {
        CommonJob commonJob = new CommonJob();
        userUtil.userAuthority();
        Date now = new Date();
        BeanUtils.copyProperties(dto, commonJob);
        commonJob.setPutUserId(UserUtil.getCurrentUserId());
        commonJob.setEndTime(CommonUtil.stringToDate2(dto.getEndTime()));
        commonJob.setIsGetting(false);
        commonJob.setStatus(3);
        commonJob.setCreateTime(now);
        commonJob.setOnline(settingService.getAudit());
        commonJobRepository.save(commonJob);
    }

    public Page<CommonJob> getAll(int page, int size, Boolean isGetting) {
        Pageable pageable = PageRequest.of(page, size);
        return commonJobRepository.findByIsGettingOrderByCreateTimeDesc(isGetting, pageable);
    }

    @Transactional
    public void updated(UpdateCommonJobDTO dto, Long id) {
        CommonJob commonJob = this.get(id);
        this.updateJob(commonJob, dto);
        //同步任务
        jobService.createJob(commonJob, dto);
    }

    private void updateJob(CommonJob commonJob, UpdateCommonJobDTO dto) {
        //预计完成时间不能超过截止时间
        Date expectedTime = CommonUtil.stringToDate2(dto.getExpectedTime());
        CommonUtil.compareTime(commonJob.getEndTime(), expectedTime);
        UserDO user = LocalUser.getLocalUser();
        commonJob.setGetUserId(user.getId());
        commonJob.setExpectedTime(expectedTime);
        commonJob.setIsGetting(true);
        commonJob.setStatus(1);
        BeanUtils.copyProperties(dto, commonJob);
        commonJobRepository.save(commonJob);
    }

    public CommonJob get(Long id) {
        CommonJob commonJob = commonJobRepository.findFirstById(id);
        if (commonJob == null) {
            throw new NotFoundException();
        }
        return commonJob;
    }

    public void updateStatus(Long id , Boolean status) {
        CommonJob commonJob = this.get(id);
        if (status) {
            commonJob.setStatus(3);
        } else {
            commonJob.setStatus(1);
        }
        commonJobRepository.save(commonJob);
    }

    /************************* 后台 *****************************/
    public void add(CreateCommonJobDTO dto) {
        CommonJobDO commonJobDO = new CommonJobDO();
        Long userId = UserUtil.getCurrentUserId();
        commonJobDO.setPutUserId(userId);
        commonJobDO.setStatus(3);
        commonJobDO.setEndTime(CommonUtil.stringToDate2(dto.getEndTime()));
        BeanUtils.copyProperties(dto, commonJobDO);
        this.save(commonJobDO);
    }

    public void edited(Long id, CreateCommonJobDTO dto) {
        CommonJobDO commonJobDO = this.getCommonJob(id);
        commonJobDO.setEndTime(CommonUtil.stringToDate3(dto.getEndTime()));
        BeanUtils.copyProperties(dto, commonJobDO);
        this.updateById(commonJobDO);
    }

    public void delete(Long id) {
        this.getCommonJob(id);
        this.getBaseMapper().deleteById(id);
    }

    public CommonJobDO getCommonJob(Long id) {
        CommonJobDO commonJobDO = this.getById(id);
        if (commonJobDO == null) {
            throw new NotFoundException();
        }
        return commonJobDO;
    }
}
